package cn.tedu.submarine;

import java.io.Serializable;
import java.util.Random;
import javax.swing.ImageIcon;
import java.awt.Graphics;
/**
 * 海洋对象
 */
public abstract class SeaObject implements Serializable {
    public static final int LIVE  = 0;
    public static final int DEAD  = 1;
    protected           int state = LIVE;

    protected int width;//宽
    protected int height;//高
    protected int x;//x坐标
    protected int y;//y坐标
    protected int speed;//移动速度

    /*专门给潜艇写的*/
    public SeaObject(int width, int height) {
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT - height - 200 + 1) + 200;
        speed = rand.nextInt(2) + 2;
    }

    /*战舰、水雷、炸弹*/
    public SeaObject(int width, int height, int x, int y, int speed) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    /*移动*/
    public abstract void move();
    public abstract ImageIcon getImage();
    public boolean  isLive(){
        return state == LIVE;//当前对象是LIVE表示或者返回true否则返回false
    }
    public boolean  isDead(){
        return state == DEAD;
    }

    public void paintImage(Graphics g){
        if (isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    /**检测潜艇越界*/
    public boolean isOutofBounds(){
        return x >= World.WIDTH;
    }
    public boolean isHit(SeaObject other){
        //假设this表示潜艇  other表示炸弹
        int x1 = this.x - other.width;//x1:潜艇的x-炸弹的宽
        int x2 = this.x + this.width;//潜艇的x+潜艇的宽
        int y1 = this.y - other.height;//潜艇的y-炸弹的高
        int y2 = this.y + this.height;//潜艇的y+潜艇的高
        int x = other.x;//炸弹的x
        int y = other.y;//炸弹的y
        return  x >= x1 && x <=x2 && y >= y1 && y <= y2;
    }

    /*海洋对象死亡*/
    public void goDead(){
        state = DEAD;
    }
}
